package com.shujia.core

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

object Demo15Actions {
  def main(args: Array[String]): Unit = {
    val conf = new SparkConf()
    conf.setMaster("local")
    conf.setAppName("Action算子演示")
    val context = new SparkContext(conf)
    //====================================================

    val studentRDD: RDD[String] = context.textFile("spark/data/students.csv")

    /**
     * 转换算子：transformation 将一个RDD转换成另一个RDD，转换算子是懒执行的，需要一个action算子触发执行
     *
     * 行动算子（操作算子）：action算子，触发任务执行。一个action算子就会触发一次任务执行
     */
    println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$")
    val studentsRDD: RDD[(String, String, String, String, String)] = studentRDD.map(_.split(","))
      .map {
        case Array(id: String, name: String, age: String, gender: String, clazz: String) =>
          println("**************************** 数加防伪码 ^_^ ********************************")
          (id, name, age, gender, clazz)
      }
    println("$$$$$$$$$$$$$$$$$$$$$$***__***$$$$$$$$$$$$$$$$$$$$$$$$$")

    // foreach其实就是一个action算子
//    studentsRDD.foreach(println)
    //    println("="*100)
    //    studentsRDD.foreach(println)

    //    while (true){
    //
    //    }

    /**
     *  collect()行动算子 主要作用是将RDD转成scala中的数据结构
     *
     */
    val tuples: Array[(String, String, String, String, String)] = studentsRDD.collect()



  }

}
